package com.example.taobaounion3.presenter.impl;

import com.example.taobaounion3.model.Api;
import com.example.taobaounion3.model.domain.TicketParams;
import com.example.taobaounion3.model.domain.TicketResult;
import com.example.taobaounion3.presenter.ITicketPresenter;
import com.example.taobaounion3.utils.LogUtils;
import com.example.taobaounion3.utils.RetrofitManager;
import com.example.taobaounion3.utils.urlUtils;
import com.example.taobaounion3.view.ITicketPagerCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class TicketPresenterlmpl implements ITicketPresenter {

    private ITicketPagerCallback mViewCallback=null;
    private TicketResult mTicketResult;
    private String mCover;

    enum LoadState{
        LOADING,SUCCESS,ERROR,NONE
    }

    private LoadState mCurrentState =LoadState.NONE;

    @Override
    public void getTicket(String title, String url, String cover) {

        this.onTicketLoading();
        mCover=urlUtils.getCoverPath(cover);
        LogUtils.d(this,"title-->"+title);
        LogUtils.d(this,"Tick cover-->"+mCover);
        String targetUrl =urlUtils.addHttpsTotUrl(url);
        LogUtils.d(this,"url-->"+targetUrl);
        //获取淘口令
        Retrofit retrofit= RetrofitManager.getOurInstance().getRetrofit();
        Api api=retrofit.create(Api.class);
        TicketParams ticketParams=new TicketParams(targetUrl,title);
        Call<TicketResult> task=api.getTick(ticketParams);
        task.enqueue(new Callback<TicketResult>() {
            @Override
            public void onResponse(Call<TicketResult> call, Response<TicketResult> response) {
                int code=response.code();
                LogUtils.d(TicketPresenterlmpl.this,"ticket result -->"+code);
                if (code== HttpURLConnection.HTTP_OK) {
                    //请求成功
                    mTicketResult = response.body();
                    LogUtils.d(TicketPresenterlmpl.this,"result -->"+mTicketResult);
                    //通知UI更新

                    onTicketLoadedSuccess();
                }else {
                    //请求失败
                    onLoadedTicketError();

                }
            }

            @Override
            public void onFailure(Call<TicketResult> call, Throwable t) {
                onLoadedTicketError();
                mCurrentState =LoadState.ERROR;
            }
        });
    }

    private void onTicketLoadedSuccess() {
        if (mViewCallback!=null) {
            mViewCallback.onTicketLoaded(mCover,mTicketResult);
        }else {
            mCurrentState =LoadState.SUCCESS;
        }
    }

    private void onLoadedTicketError() {
        if (mViewCallback!=null) {
            mViewCallback.onError();
        }else {
            mCurrentState = LoadState.ERROR;
        }
    }

    private void onTicketLoading() {
        if (mViewCallback!=null) {
            mViewCallback.onLoading();
        }else {
            mCurrentState =LoadState.LOADING;
        }
    }

    @Override
    public void registerViewCallback(ITicketPagerCallback callback) {
        if (mCurrentState!= LoadState.NONE) {
            //说明状态已经改变，更新UI
            if (mCurrentState== LoadState.SUCCESS) {
                onTicketLoadedSuccess();
            }else if (mCurrentState==LoadState.ERROR){
                onLoadedTicketError();
            }else if (mCurrentState==LoadState.LOADING)
                onTicketLoading();

        }
        this.mViewCallback=callback;
    }


    @Override
    public void unregisterViewCallback(ITicketPagerCallback callback) {
        this.mViewCallback=null;
    }
}
